/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison.websitereader.discountmerchant;

import java.io.IOException;
import javax.swing.JLabel;
import jxl.write.WriteException;
import pricecomparison.PriceComparisonView;

/**
 *
 * @author Jim Simon
 */
public class DM_HomePageReader {
    public String URL = "http://www.discount-merchant.com/";
    public String GET = "?searching=Y&sort=7&cat=3002&show=25&page=";
    
    public void beginSearch(String page, boolean newSheet, JLabel l) throws jxl.write.WriteException, java.io.IOException {
        try {
            // Check if a new sheet is needed
            if (newSheet)
                PriceComparisonView.excelWriter.newSheet(PriceComparisonView.excelWriter.MFER_DISCOUNTMERCHANT);
            
            // Instantiate an ItemListReader for the first page
            DM_ItemListReader group = new DM_ItemListReader(URL + page + GET + "1");
            
            // Get the total number of pages for this brand
            int numPages = group.getNumberOfPages();

            if (numPages == -1)
                return;
            
            // Do first page
            group.beginSearch(l);
            
            // Do rest of pages
            for (int i = 2; i <= numPages; i++) {
                group = new DM_ItemListReader(URL + page + GET + i);
                group.beginSearch(l);
            }
        } catch (jxl.write.WriteException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
    }
}
